/* $RCSFile: IntegerDataType.cs $
 * Apache license v2 2011 by Christian Surieux
 * $Source: ?/ADNCRM.Core/UI/DataTypes/IntegerDataType.cs $
 * $log$
 * Revision 6 2010/08/06 19:56:29  christian.surieux
 *   Added Log to All cs files
 */

using System;
using System.Diagnostics;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Globalization;

using AddonNice.Diagnostics;
using AddonNice.UI;
using AddonNice.Configuration;

using Telerik.Web.UI;

namespace AddonNice.UI.DataTypes
{
	/// <summary>
	/// IntegerDataType
	/// </summary>
	public class IntegerDataType : NumericDataType
	{
		public IntegerDataType()
		{
			InnerDataType		=   PropertiesDataType.Integer;
		}

		protected override void InitializeComponents()
		{
            if ( DataTypeTraceSwitch.Sw.TraceVerbose )
                Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]IntegerDataType InitializeComponents Creating TextBox for datatype description: {0}, UseRadControl: {1}", Description,_UsesRadControl),DataTypeTraceSwitch.Sw.Info);
            if (!UseRadControl)
            {
                base.InitializeComponents();
                return;
            }
            RadNumericTextBox tx                        =   new RadNumericTextBox();
            tx.MinValue                                 =   int.MinValue+1;
            tx.MaxValue                                 =   int.MaxValue-1;
            tx.NumberFormat.DecimalDigits               =   0;
            tx.ShowSpinButtons                          =   true;
            tx.IncrementSettings.Step                   =   1;
            tx.IncrementSettings.InterceptArrowKeys     =   true;
            tx.IncrementSettings.InterceptMouseWheel    =   true;
            
            tx.Width                                    =   controlWidth;
            if (HandleChanges)
            {
                if ( DataTypeTraceSwitch.Sw.TraceVerbose )
                    Trace.WriteLine( "[V]IntegerDataType InitializeComponents HandleChanges set",DataTypeTraceSwitch.Sw.Info);
                tx.AutoPostBack                         =   true;
                tx.TextChanged                          +=  new EventHandler(tx_TextChanged);
            }
            if ( ReadOnly )
                tx.Enabled                              =   false;
            innerControl                                =   tx;
            _IsInitialized                              =   true;
		}

		public override string Val
		{
			get
			{
				return innerValue;
			}
			set
			{
				try
				{
					innerValue                          = Int32.Parse("0" + value).ToString();
				}
				catch (Exception ex)
				{
					if ( GlobalTraceSwitch.Sw.TraceError )
                        Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]IntegerDataType Set value: {0}, ex: {1}",value,ex),DataTypeTraceSwitch.Sw.Info);
                    throw;
				}
			}
		}

		public override string Description
		{
			get
			{
				return "Integer";
			}
		}
	}
}